package com.ndp.fb.creative.fetchdata;


//import com.ndp.fb.config.BaseConfig;
//import com.ndp.fb.config.ConfigCentre;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.thetransactioncompany.cors.CORSFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * ��
 * Created by evan.wan on 2015/5/4.
 */
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer
{
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${spring.data.dataSource.url}")
    private String url;

    @Value("${spring.data.dataSource.username}")
    private String username;

    @Value("${spring.data.dataSource.password}")
    private String password;

    @Value("${spring.data.dataSource.driverClassName}")
    private String driverClassName;

    @Value("${mongodbClient.url}")
    private String mongodbUri;

    @Value("${spring.data.mongodb.db}")
    private String mongodbDb;





    public static void main(String[] args) throws Exception {
//        BaseConfig.DEV = true;
//        ConfigCentre.RegisterAppIds(CreativeApplication.class);
        //String[] argsparams = {"springbootweb"};
        SpringApplication.run(Application.class);

    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }


    @Bean
    public FilterRegistrationBean corsFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        CORSFilter corsFilter = new CORSFilter();
        registrationBean.setFilter(corsFilter);
        registrationBean.setEnabled(true); //filter
        registrationBean.setOrder(1);
        return registrationBean;
    }

    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverterBean(){
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setCharset(Charset.forName("UTF-8"));
        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.TEXT_HTML); //
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
        return fastJsonHttpMessageConverter;
    }

    @Bean
    public HttpMessageConverters customConverters(FastJsonHttpMessageConverter fastJsonHttpMessageConverterBean) {
        HttpMessageConverter<?> a = fastJsonHttpMessageConverterBean;
        return new HttpMessageConverters(a);
    }

    @Bean
    public DruidDataSource dataSource() throws SQLException
    {
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);

        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnReturn(true);
        dataSource.setTestWhileIdle(true);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTimeBetweenEvictionRunsMillis(1800000l);
        dataSource.setMinEvictableIdleTimeMillis(1800000l);
        dataSource.setPoolPreparedStatements(true);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
        dataSource.setInitialSize(10);
        dataSource.setMinIdle(5);
        dataSource.setMaxActive(50);
        dataSource.setMaxWait(60000);
        dataSource.setFilters("stat");
        dataSource.init();
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DruidDataSource dataSource){
        return  new JdbcTemplate(dataSource);
    }

    @Bean
    public DB mongoDBClient() throws Exception {
        Mongo mongo = new MongoClient(new MongoClientURI(mongodbUri)); //mongodb://172.30.10.121:27017
        DB db = mongo.getDB(mongodbDb);
        return db;
    }

}

